package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() {
        // 设置key value
        redisTemplate.opsForValue().set("name", "张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        // 设置过期时间
        redisTemplate.opsForValue().set("token", "123456789",10, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        // user-大key    name,age-小key
        redisTemplate.opsForHash().put("user","name","张三");
        redisTemplate.opsForHash().put("user","age","18");
        List<Object> values = redisTemplate.opsForHash().values("user");
        System.out.println(values);

        // Set集合
        redisTemplate.opsForSet().add("set1","1","2","3","4");
        redisTemplate.opsForSet().add("set2","a","b","c","d");
        Set<String> members = redisTemplate.opsForSet().members("set1");
        System.out.println(members);
        long size = redisTemplate.opsForSet().size("set1");
        System.out.println(size);

        // 列表
        redisTemplate.opsForList().leftPushAll("list","1","2","3");
        redisTemplate.opsForList().leftPush("list","4");
        //
        System.out.println(redisTemplate.opsForList().range("list",0,-1));
    }


}